// pages/cart/cart.js
import api from '../../utils/api';
import store from '../../utils/store'
import common from '../../utils/common'
import message from '../../utils/message';
Page({

  /**
   * 页面的初始数据
   */
  data: {

    product_list:[],
    hasList:false,
    totalPrice:0,
    selectAllStatus:true,
    static_url:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    


    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    const app = getApp();
    this.setData({
      static_url:app.globalData.static_url
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    common.updateCartNums();

    var me = this;
    api.cart.getProductList( {} , function( response){
      /*
      me.setData({
        carts: response.data
      })
      */


      me.setData({
        product_list:response.data.product_list,
        totalPrice: response.data.total
      })
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getTotalPrice(){
    let carts = this.data.carts;
    let total = 0;

    for( var i=0; i<carts.length ; i++){
      if(carts[i].selected){

        total +=carts[i].nums * carts[i].price
      }
    }
    this.setData({
      carts:carts,
      totalPrice:total.toFixed(2)
    })
  },
  selectList(e){
    var index = e.currentTarget.dataset.index;
    let carts = this.data.carts;

    const selected = carts[index].selected;


    carts[index].selected = !selected

    this.setData({
      carts:carts
    });
    this.getTotalPrice();
  },
  selectAll(e){
    var selectAllStatus = this.data.selectAllStatus;
    selectAllStatus = !selectAllStatus;

    var carts = this.data.carts;
    for( var i=0;i<carts.length ; i++ ){
      carts[i].selected = selectAllStatus;
    }

    this.setData({
      selectAllStatus:selectAllStatus,
      carts:carts
    })

    this.getTotalPrice();
  },
  addCount(e){

    /*
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    let nums = carts[index].nums;
    nums = nums+1;
    carts[index].nums = nums;
    this.setData({
      carts: carts
    });
    this.getTotalPrice();

    */

   var id = e.currentTarget.dataset.id;

   var params = {}
   params.id = id;
   params.product_nums = 1;

   var me = this;
   api.cart.addCartProduct(  params , function(response){
     if(response.status==1){

        me.getProductList();
        message.toast(response.message)
     }else{
        //处理异常数据  例如库存不足等等 

        message.show_error(response.message);
     }
   })
  },
  minusCount(e){

    var me = this;
    var id = e.currentTarget.dataset.id;
      var params = {}
      params.id = id;

      api.cart.reduceProduct( params , function( response ){

        if(response.status==1){
          me.getProductList();
          message.toast(response.message)
        }else{
          message.show_error(response.message);
        }
      })
  },
  deleteCartProduct(e){
    //从购物车删除商品
    var id = e.currentTarget.dataset.id;
    var me = this;

    api.cart.removeProduct( { id:id} , function( response ){

      if(response.status==1){
        me.getProductList();
        message.toast(response.message)        
      }else{
        message.show_error(response.message);
      }
    })



    /*
    const index = e.currentTarget.dataset.index;
    let carts = this.data.carts;
    carts.splice(index ,1);
    this.setData({
      carts: carts
    });
    if(!carts.length){
      this.setData({
        hasList:false
      })
    }else{
      this.getTotalPrice();
    }

    */
  },
  order_create:function(){
    wx.navigateTo({
      url: '/pages/order-create/order-create?type=cart'
    })



  },
  getProductList:function(){
    var me = this;
    api.cart.getProductList( {} , function( response){
      var sums = response.data.cart_product_sums

      store.set_cart_product_sums(sums);



      if(sums>0){
        wx.setTabBarBadge({
          index: 2,
          text: ""+sums
        })

     }else{
        wx.removeTabBarBadge({
          index: 2,
        })

     }


      me.setData({
        product_list:response.data.product_list,
        totalPrice: response.data.total
      })
    })
  }


})